// 全局的 axios 默认值
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

//添加
function post(url, data, auth) {
  return new Promise((resolve, reject) => {
    axios({
      url,
      method: "post",
      headers: { "Content-type": "application/x-www-form-urlencoded" },
      data,
      auth,
    }).then(function (resData) {
      resolve(resData);
    });
  });
}
// `auth` 表示应该使用 HTTP 基础验证，并提供凭据
// 这将设置一个 `Authorization` 头，覆写掉现有的任意使用 `headers` 设置的自定义 `Authorization`头
/* 用法
auth: {
    username: 'janedoe',
    password: 's00pers3cret'
  }, 
*/

//获取
/* params: {
  ID: 12345;
} */
function get(url, params, auth) {
  return new Promise((resolve, reject) => {
    axios({
      url,
      method: "get",
      headers: { "Content-type": "application/x-www-form-urlencoded" },
      params,
      auth,
    }).then(function (resData) {
      resolve(resData);
    });
  });
}

//put 修改覆盖全部原数据
function put(url, data) {
  return new Promise((resolve, reject) => {
    axios({
      url,
      method: "put",
      headers: { "Content-type": "application/x-www-form-urlencoded" },
      data,
    }).then(function (resData) {
      resolve(resData);
    });
  });
}

//patch 局部修改，其他不变
//url后加需要修改的数据的id 如："http://localhost:3000/cart/20"
/* data: {
  firstName: "Fred";
} */
function patch(url, data) {
  return new Promise((resolve, reject) => {
    axios({
      url,
      method: "PATCH",
      headers: { "Content-type": "application/x-www-form-urlencoded" },
      data,
    }).then(function (resData) {
      resolve(resData);
    });
  });
}

//删除 url后加需要删除的数据的id 如："http://localhost:3000/cart/20"
function del(url) {
  return new Promise((resolve, reject) => {
    axios({
      url,
      method: "delete",
    }).then(function (resData) {
      resolve(resData);
    });
  });
}

/* 用法：
async function fn() {
    let resData = await put("http://localhost:3000/cart/50", {
      name:"李四",
      age:18
    });
    console.log(resData);
  }
  fn(); 
*/
